package com.asktgapp.user.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.model.ProvinceVO;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;
import com.asktgapp.modulebase.common.util.Util;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.PropertyListParser;

import java.util.ArrayList;
import java.util.List;

/**
 * time:2018/1/29
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class ChooseCityFragment extends BaseFragment {
    private RecyclerView mProviceRecyclerView, mCityRecyclerView;
    private BaseAdapter mProviceAdapter, mCityAdapter;
    private List<ProvinceVO> list = new ArrayList<>();
    private FrameLayout currentTv = null;
    private FrameLayout secondTv = null;
    private int mAreaId;
    private String mAreaName;
    private int mChoosePositon = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_choose_city, container, false);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        mProviceRecyclerView = rootView.findViewById(R.id.left_recycler);
        mCityRecyclerView = rootView.findViewById(R.id.right_recycler);
        mProviceAdapter = new BaseAdapter(getActivity()) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, final int viewType) {
                BaseViewHolder holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_province, parent, false), new BaseViewHolder.Callbacks() {
                    @Override
                    public void bind(BaseViewHolder holder, Object data) {
                        if (data instanceof ProvinceVO) {
                            TextView province = holder.getView(R.id.textView);
                            ProvinceVO provinceVO = (ProvinceVO) data;
                            province.setText(provinceVO.getAreaname());
                            if (viewType == 1) {
                                province.setBackgroundColor(Color.WHITE);
                            } else {
                                province.setBackgroundColor(Color.parseColor("#e0e0e0"));
                            }
                        }
                    }

                    @Override
                    public void onItemClick(View view, int position) {
                        mChoosePositon = position;
                        mProviceAdapter.notifyDataSetChanged();
                        mProviceRecyclerView.smoothScrollToPosition(mChoosePositon);
                        ProvinceVO provinceVO = (ProvinceVO) mProviceAdapter.getData().get(position);
                        mCityAdapter.setData(provinceVO.getCitys());
                        mCityRecyclerView.smoothScrollToPosition(0);
                        mAreaName = provinceVO.getAreaname();
                        mAreaId = provinceVO.getAreaid();

                    }
                });
                return holder;
            }

            @Override
            public int getItemViewType(int position) {
                if (position == mChoosePositon) {
                    return 1;
                } else return 0;
            }
        };
        mCityAdapter = new BaseAdapter(getActivity()) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_city, parent, false), new BaseViewHolder.Callbacks() {
                    @Override
                    public void bind(BaseViewHolder holder, Object data) {
                        if (data instanceof ProvinceVO.City) {
                            TextView city = holder.getView(R.id.city);
                            ProvinceVO.City bean = (ProvinceVO.City) data;
                            city.setText(bean.getAreaname());
                        }
                    }

                    @Override
                    public void onItemClick(View view, int position) {
                        ProvinceVO.City city = (ProvinceVO.City) mCityAdapter.getData().get(position);
                        Intent intent = new Intent();
                        if (city.getAreaid() == 110100) {
                            mAreaId = 110000;
                        }
                        if (Util.isEmpty(mAreaName)) {
                            mAreaName = "北京市";
                        }
                        intent.putExtra("city", city.getAreaname());
                        intent.putExtra("cityId", city.getAreaid() + "");
                        intent.putExtra("area", mAreaName);
                        intent.putExtra("areaId", mAreaId + "");
                        getActivity().setResult(Activity.RESULT_OK, intent);
                        getActivity().finish();
                    }
                });
                return holder;
            }
        };
        mCityRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mProviceRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mCityRecyclerView.setAdapter(mCityAdapter);
        mProviceRecyclerView.setAdapter(mProviceAdapter);
    }


    @Override
    public void loadData() {
        getProivnceAndCity();
        visibleLoading();
        mProviceRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                inVisibleLoading();
            }
        }, 800);
    }

    private void getProivnceAndCity() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    NSArray ary = (NSArray) PropertyListParser.parse(getActivity().getAssets().open("City.plist"));
                    for (int i = ary.count() - 1; i >= 0; i--) {
                        NSDictionary dic = (NSDictionary) ary.objectAtIndex(i);
                        ProvinceVO provinceVO = new ProvinceVO();
                        List<ProvinceVO.City> city = new ArrayList<>();
                        String areaid = dic.objectForKey("areaid").toJavaObject().toString();
                        String name = dic.objectForKey("areaname").toJavaObject().toString();
                        NSArray citys = (NSArray) dic.objectForKey("citys");
                        for (int j = citys.count() - 1; j >= 0; j--) {
                            NSDictionary secDic = (NSDictionary) citys.objectAtIndex(j);
                            String secAreaid = (secDic.objectForKey("areaid")).toJavaObject().toString();
                            String secName = (secDic.objectForKey("areaname")).toJavaObject().toString();
                            ProvinceVO.City bean = new ProvinceVO.City();
                            bean.setAreaid(Integer.parseInt(secAreaid));
                            bean.setAreaname(secName);
                            city.add(bean);
                        }
//                        ProvinceVO.City all = new ProvinceVO.City();
//                        all.setAreaid(Integer.parseInt(areaid));
//                        all.setAreaname("全部");
//                        city.add(0, all);
                        provinceVO.setAreaid(Integer.parseInt(areaid));
                        provinceVO.setAreaname(name);
                        provinceVO.setCitys(city);
                        list.add(provinceVO);
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mProviceAdapter.setData(list);
                                mCityAdapter.setData(list.get(0).getCitys());
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
